package com.answer.scoring;

import com.answer.excepction.BaseException;
import com.answer.model.entity.App;
import com.answer.model.entity.Question;
import com.answer.model.entity.UserAnswer;
import com.answer.service.AppService;
import com.answer.service.QuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;



@Service
public class ScoringStrategyContext {
    @Resource
    private List<ScoringStrategy> scoringStrategyList;


    @Resource
    private AppService appService;


    @Resource
    private QuestionService questionService;

    public UserAnswer doScore(List<String> choices, Long questionId){

        Question question = questionService.getById(questionId);
        App app = appService.getById(question.getAppId());
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();

        for (ScoringStrategy strategy : scoringStrategyList){
            if (strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                ScoringStrategyConfig scoringStrategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (scoringStrategyConfig.appType() == appType && scoringStrategyConfig.scoreType()==scoringStrategy){
                    return strategy.doScore(choices,question);
                }
            }
        }
        throw new BaseException("应用配置错误");
    }

}
